﻿using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Temp.Database;
using Temp.Model;

namespace Temp.Controller
{
    class ItemController
    {
        private SQLiteFactory sqlf;
        private SQLite.SQLiteConnection conn;
        private List<Item> items;
        private List<Ontology> itemOnt;

        public ItemController()
        {
            sqlf = new SQLiteFactory();
        }

        public List<Temp.Model.Item> getItems(string namePlace)
        {
            items = new List<Item>();
            itemOnt = new List<Ontology>();
            conn = sqlf.getConnection();
            itemOnt = conn.Query<Ontology>("SELECT *  FROM Item_Ont WHERE relation = \"located at\" AND concept2 = \"" + namePlace + "\"").ToList();
            foreach (Ontology io in itemOnt)
            {
                System.Diagnostics.Debug.WriteLine(io.concept1);
                items.Add(conn.Query<Item>("SELECT * FROM Item WHERE name = \"" + io.concept1.ToLower() + "\"").Single());
            }
            conn.Close();
            return items;
        }

        public List<Ontology> getNeededItems(string relation, string concept2) {
            itemOnt = new List<Ontology>();
            conn = sqlf.getConnection();
            itemOnt = conn.Query<Ontology>("SELECT * FROM Item_Ont WHERE relation = ? COLLATE NOCASE AND concept2 = ? COLLATE NOCASE", relation, concept2);
            return itemOnt;
        }

        public List<Temp.Model.Item> getAllItems()
        {
            PlaceController pCtr = new PlaceController();
            items = new List<Item>();

            conn = sqlf.getConnection();
            items = conn.Query<Item>("SELECT * FROM Item" ).ToList();
            String str = "";
            foreach (Item it in items)
            {
                try
                {
                    str = conn.Query<Ontology>("SELECT *  FROM Item_Ont where concept1='" + it.name[0].ToString().ToUpper() + it.name.Remove(0, 1) + "' and relation ='locatedAt'").Single().concept2;
                    it.idPlace = pCtr.getPlaceIdByString(str);
                }

                catch (Exception e) { 
                
                }
            }
            conn.Close();
            return items;
        }


        public int getPlaceId(string item) {
            PlaceController pCtr = new PlaceController();
            conn = sqlf.getConnection();
            try
            {
                string str = conn.Query<Ontology>("SELECT *  FROM Item_Ont where concept1='" + item[0].ToString().ToUpper() + item.Remove(0, 1) + "' and relation ='locatedAt'").Single().concept2;
                return pCtr.getPlaceIdByString(str);
            }
            catch (Exception e)
            {

            }
            return 0;
        }

        public List<Rectangle> allObjectRect(string namePlace)
        {
            List<Temp.Model.Item> listobj = getItems(namePlace);
            List<Rectangle> rects = new List<Rectangle>();

            for (int i = 0; i < listobj.Count; i++)
                rects.Add(new Rectangle(listobj.ElementAt(i).x, listobj.ElementAt(i).y,
                   54, 87)); //temporary size

            return rects;
        }


        public List<Ontology> getAttributeValue(string tag, string name)
        {
            conn = sqlf.getConnection();
            System.Diagnostics.Debug.WriteLine("tag" + tag + " name:" + name);
            List<Ontology> iOntList = conn.Query<Ontology>("SELECT * FROM Item_Ont WHERE relation = ? AND concept1 = ? COLLATE NOCASE", tag, name).ToList();
            System.Diagnostics.Debug.WriteLine("cOUNT LIST:" + iOntList.Count);
            conn.Close();
            return iOntList;
        }



        public Item getItem(string itemName)
        {
            conn = sqlf.getConnection();
            System.Diagnostics.Debug.WriteLine("ITEM NAME:" + itemName);
            Item i = conn.Query<Item>("SELECT * FROM Item WHERE name = ?", itemName.ToLower()).Single();
            conn.Close();
            return i;
        }

        /// <summary>
        /// getItems excluding the ones na nasa inventory
        /// </summary>
        /// <param name="namePlace"></param>
        /// <param name="inv"></param>
        /// <returns></returns>
        public List<Temp.Model.Item> updateItems(string namePlace, List<Item> inv)
        {
            List<Temp.Model.Item> gami = getItems(namePlace);
            List<Temp.Model.Item> tempo = new List<Item>();

            for (int i = 0; i < gami.Count; i++)
            {
                if (!isInInventory(inv, gami.ElementAt(i)))
                    tempo.Add(gami.ElementAt(i));
            }

            return tempo;
        } //end of getItem2

        private Boolean isInInventory(List<Item> inv, Item gamito)
        {
            Boolean isThere = false;

            for (int i = 0; i < inv.Count; i++)
            {

                if (gamito.name.Equals(inv.ElementAt(i).name))
                    isThere = true;
            }



            return isThere;
        } //end of isInInventory


        public Ontology checkKeyItem(string itemName) {
            conn = sqlf.getConnection();
            try{
                System.Diagnostics.Debug.WriteLine("ITEM NAME IN CONTROLLER = " + itemName);
                Ontology o = conn.Query<Ontology>("SELECT * FROM Story_Ont WHERE concept1 = ?  AND relation = ? AND concept2 = ?", itemName.ToLower(), "isA", "key item").Single();
                System.Diagnostics.Debug.WriteLine("ITEM NAME IN CONTROLLER = " + o.concept2);
                
                return o;
            }
            catch (Exception e){
                return null;
            }
            
        }

        /*
         
         public List<Ontology> getObjectAttributes(int idObject) {
            conn = sqlf.getConnection();
            objAttribList = (conn.Table<Ontology>().Where(o => o.id == idObject)).ToList();
            conn.Close();
            return objAttribList;
        }
        */
    }
}
